package com.pe.pgn.clubpgn.webapp.controller.tipovehiculo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpbTipoVehiculo;
import com.pe.pgn.clubpgn.service.TipoVehiculoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;


@Controller
@RequestMapping("/tipovehiculo/**")
public class TipoVehiculoController extends BaseFormController {
	
	@Autowired
	private TipoVehiculoService tipoVehiculoService;
    	
	@RequestMapping(value = "/tipovehiculolist.html")
    public void listarTipoVehiculo(Model model) {

		List<ClpbTipoVehiculo> tiposVehiculo = tipoVehiculoService.obtenerTiposVehiculo();
		model.addAttribute("tiposVehiculo", tiposVehiculo);
    }
	
	@RequestMapping(value = "/tipovehiculoform.html", method = RequestMethod.GET)
	public @ModelAttribute("tipoVehiculo") ClpbTipoVehiculo verTipoVehiculo(
				@RequestParam(value = "id", required = false) Long id, Model model) {

		if (id != null) {
			ClpbTipoVehiculo tipoVehiculo = tipoVehiculoService.obtenerTipoVehiculo(id);
			return tipoVehiculo;
		}
		return new ClpbTipoVehiculo();
	}
	
	@RequestMapping(value = "/tipovehiculoform.html", method = RequestMethod.POST)
	public String guardarTipoVehiculo(@ModelAttribute("tipoVehiculo") ClpbTipoVehiculo tipoVehiculo,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {

			if (result.hasErrors()) {
				model.addAttribute("tipoVehiculo", tipoVehiculo);
				return "/tipovehiculo/tipovehiculoform";
			}

			tipoVehiculoService.guardarTipoVehiculo(tipoVehiculo);
			saveMessage(request, getText("tipovehiculoform.added", tipoVehiculo.getDeTipoVehiculo(), 
					request.getLocale()));
			return "redirect:tipovehiculolist.html";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/tipovehiculo/tipovehiculoform";
		}

	}	
	
	@RequestMapping(value = "/eliminartipovehiculo.html")
	public String eliminarTipoVehiculo(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request, Model model) {

		try {

			tipoVehiculoService.eliminarTipoVehiculo(id);
			saveMessage(request, getText("tipovehiculoform.deleted", request.getLocale()));
			return "redirect:tipovehiculolist.html";
			
		} catch (Exception e) {
			ClpbTipoVehiculo tipoVehiculo = tipoVehiculoService.obtenerTipoVehiculo(id);
			model.addAttribute("tipoVehiculo", tipoVehiculo);
			saveError(request, getText("errors.general", request.getLocale()));
			return "/tipovehiculo/tipovehiculoform";
		}

	}		
}
